package com.example.image.glide

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.MultiTransformation
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.image.R
import jp.wasabeef.glide.transformations.BlurTransformation
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

/**
 * Glide图片加载工具
 */
object GlideImgLoader {
    //普通图片
    fun loadImage(context: Context, url:String, imageView: ImageView){
        Glide.with(context)
            .load(url)
            .error(ColorDrawable(Color.YELLOW))
            .placeholder(ColorDrawable(Color.GRAY))
            .thumbnail(0.5F)//设置缩略图
            .skipMemoryCache(false)//跳过内存缓存
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .transform(CenterCrop())
            .into(imageView)
    }
    //圆形图片
    fun loadCircleImage(context:Context,url:String,imageView: ImageView,radio:Int){
        Glide.with(context)
            .load(url)
            .error(ColorDrawable(Color.YELLOW))
            .placeholder(ColorDrawable(Color.GRAY))
            .thumbnail(0.5F)//设置缩略图
            .skipMemoryCache(true)//跳过内存缓存
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .transform(CenterCrop(),RoundedCorners(radio))
            .into(imageView)
    }
    //圆角图片
    fun loadCornerImage(context:Context,url:String,imageView: ImageView,radius:Int){
        Glide.with(context)
            .load(url)
            .error(ColorDrawable(Color.YELLOW))
            .placeholder(ColorDrawable(Color.GRAY))
            .skipMemoryCache(true)
            .transform(CenterCrop(),CircleCrop())
            .into(imageView)
    }
    //加载本地资源图片
    fun loadResImage(view:View,resId:Int,imageView: ImageView){
        Glide.with(view)
            .load(resId)
            .skipMemoryCache(true)
            .transform(CenterCrop())
            .into(imageView)
    }
    //毛玻璃效果
    fun loadBlur(context: Context,url: String,imageView: ImageView){
        Glide.with(context)
            .load(url)
            .placeholder(ColorDrawable(Color.GRAY))
            .error(ColorDrawable(Color.YELLOW))
            .thumbnail(0.5F)
            .skipMemoryCache(false)
            .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            .apply(RequestOptions.bitmapTransform(MultiTransformation<Bitmap>(
                BlurTransformation(10,3),
                RoundedCornersTransformation(128,0,RoundedCornersTransformation.CornerType.BOTTOM)
            )))
            .into(imageView)
    }
}